package com.chidopi.cdf.security;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;


public class SecurityManager implements UserDetailsService {

	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		// Don't compare password because SpringSecurity handle it.
		ArrayList<DefaultUser> userList = new ArrayList<DefaultUser>();
		DefaultUser user1 = new DefaultUser("tom", "cat");
		Set<Authority> roles = new HashSet<Authority>();
		Authority authority = new Authority();
		authority.setAuthority("ROLE_USER");
		authority.setId(1L);
		authority.setUsername("tom");
		roles.add(authority);
		user1.setRoles(roles);
        userList.add(user1);
        for (int i = 0; i < userList.size(); i++) {
        	DefaultUser user = (DefaultUser) userList.get(i);
            if (user.getUsername().equals(username))
                return user;
        }
        throw new UsernameNotFoundException("User " + username
                + " has no GrantedAuthority");
	}

}
